package 一对多通讯;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class SeverSocketDemo {
	public static void main(String[] args) throws IOException {
		ServerSocket ss=new ServerSocket(9099);
		while(true){
			Socket s = ss.accept();
			Server ser=new Server(s);
			Thread t=new Thread(ser);
			t.start();
		}
	}

}
class Server implements Runnable {
	Socket s;
	public Server(Socket s){
		this.s=s;
	}
	public void run() {
		try {
			DataInputStream dis=new DataInputStream(s.getInputStream());
			DataOutputStream dos=new DataOutputStream(s.getOutputStream());
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			String info;
			while(true){
				info=dis.readUTF();
				if(info=="886"){
					break;
				}
				System.out.println("客户端："+info);
				info=br.readLine();
				if(info=="886"){
					break;
				}
				dos.writeUTF(info);
			}
			br.close();
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("服务器数据异常！");
		}
	}
}